package com.filldream.fastboot.core.job;

import com.alibaba.fastjson2.JSONObject;
import com.filldream.fastboot.base.mapper.StockLastMapper;
import com.filldream.fastboot.common.constant.enums.StockFstEnum;
import com.filldream.fastboot.common.entity.db.StockLast;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
public class SimulateStockJob {

    private static final Integer daySum = 30;

    private final StockLastMapper stockLastMapper;

    @Scheduled(cron = "0/1 * * * * ?")
    public void autoGenSettOrder() {
        StockLast stockBaseInfo = stockLastMapper.getWaitLastList();
        if( stockBaseInfo != null ){
            log.info("开始处理股票数据：{}", stockBaseInfo.getSearchCode());
            BigDecimal winningRate = deal(stockBaseInfo.getSearchCode());
            stockBaseInfo.setWinningRate(winningRate);
            stockBaseInfo.setId(null);
            stockLastMapper.insert(stockBaseInfo);
        }
    }

    public static BigDecimal deal(String searchCode){
        List<String> dayList = getDayList(searchCode);
        if(ObjectUtils.isNotEmpty(dayList)){
            return getWinningRate( dayList );
        }
        return BigDecimal.ZERO;
    }

    public static List<String> getDayList(String searchCode){
        List<String> everyDayList = new LinkedList<>();
        JSONObject jsonObject = StockFstEnum.getKLine(3, searchCode);
        String marketData = jsonObject.getString("marketData");
        if(StringUtils.isNotBlank(marketData)){
            String[] dayList = marketData.split(";");
            for (String itemDay : dayList) {
                everyDayList.add(itemDay);
            }
            Collections.reverse(everyDayList);
            if( everyDayList.size() > daySum ){
                everyDayList = everyDayList.subList(0, daySum);
            }
            Collections.reverse(everyDayList);
        }
        return everyDayList;
    }

    public static BigDecimal getWinningRate(List<String> everyDayList){
        String[] firstDayStrList = everyDayList.get(0).split(",");
        BigDecimal firstOpenPrice = new BigDecimal(firstDayStrList[2]);
        BigDecimal firstClosePrice = new BigDecimal(firstDayStrList[3]);
        BigDecimal firstHighPrice = new BigDecimal(firstDayStrList[5]);
        BigDecimal firstLowPrice = new BigDecimal(firstDayStrList[6]);


        //开始处理每天的数据，找到符合算法的股票
        int okCount = 0;
        for (int i = 0; i < everyDayList.size(); i++) {
            if( i == 0 ){continue;}
            String itemDay = everyDayList.get(i);
            //获取最高价和最低价，获取开盘价和收盘价
            String[] dayData = itemDay.split(",");
            BigDecimal openPrice = new BigDecimal(dayData[2]);
            BigDecimal closePrice = new BigDecimal(dayData[3]);
            BigDecimal highPrice = new BigDecimal(dayData[5]);
            BigDecimal lowPrice = new BigDecimal(dayData[6]);
            BigDecimal targetPrice = (firstClosePrice.add(new BigDecimal("0.05"))).multiply(new BigDecimal("1.01"));
            if( openPrice.compareTo(targetPrice) >= 0 || closePrice.compareTo(targetPrice) >= 0 || highPrice.compareTo(targetPrice) >= 0 || lowPrice.compareTo(targetPrice) >= 0 ){
                okCount += 1;
            }
            firstOpenPrice = openPrice;
            firstClosePrice = closePrice;
            firstHighPrice = highPrice;
            firstLowPrice = lowPrice;
        }
        BigDecimal result = new BigDecimal(okCount + "").divide(new BigDecimal(daySum + ""), 4, BigDecimal.ROUND_DOWN);
        return result;
    }



}
